package com.fingard.dsp.bank.directbank.icbc02;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.ReqQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.RetAtsQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.detail.RetAtsRefundDetail;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;

public class ICBC02QryRefund extends ICBC02Base implements IQryRefund {
    @Override
    public RetAtsQryRefund d_qry_refund(ReqQryRefund p_req) throws Exception {
        RetAtsQryRefund retAts = new RetAtsQryRefund(p_req.reqHeader);
        retAts.writeStart();

        String tmpRespCode = "";
        String tmpRespInfo = "";

        int tmpSuccCount = 0;
        BigDecimal tmpTotalAmt = new BigDecimal("0");
        Statement statement = null;
        ResultSet rs = null;
        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req.getQryStartDate());

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req.getQryEndDate());
            tmpCalendarTo.add(Calendar.DAY_OF_MONTH, 1);

            while (tmpCalendarFrom.compareTo(tmpCalendarTo) == -1) {
                Date tmpQryDate = tmpCalendarFrom.getTime();
                Connection conn = FGBiz.getLocalDbConn();
                statement = conn.createStatement();
                String queryStr = "select * from ICBCPAYITEM where ownact='"+p_req.payAct+"' and refunddate='"+Format.Date10Format.format(tmpQryDate)+"' and isrefund=1";
                rs = statement.executeQuery(queryStr);
                while(rs.next()) {
                    RetAtsRefundDetail tmpRetDetail = new RetAtsRefundDetail();

                    tmpRetDetail.askNum = rs.getString("asknumber");
                    tmpRetDetail.reqBatchNO = rs.getString("reqbatchno");
                    tmpRetDetail.payAct = p_req.payAct;
                    tmpRetDetail.payActName = "";
                    tmpRetDetail.recAct = rs.getString("oppact");
                    tmpRetDetail.recActName = rs.getString("oppactname");
                    tmpRetDetail.amount = Format.DecimalAmtFormat.format(Double.parseDouble(rs.getString("amount")));
                    tmpRetDetail.refundTicketDate = Format.Date10Format.format(tmpCalendarFrom.getTime());
                    tmpRetDetail.submitTime = Format.DateTimeFormat.format(tmpCalendarFrom.getTime());
                    tmpRetDetail.refundTicketReason = rs.getString("refundreason");

                    tmpSuccCount++;
                    tmpTotalAmt = tmpTotalAmt.add(new BigDecimal(tmpRetDetail.amount));
                    retAts.writeRetDetailOnly(tmpRetDetail);
                }

                tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
            }

            retAts.totleAmount = Format.DecimalAmtFormat.format(tmpTotalAmt);
            retAts.totleNum = String.valueOf(tmpSuccCount);
            retAts.writeTotalInfo();
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRespCode = "-1";
            tmpRespInfo = ex.getMessage();
            WriteBankLogLn(ex);
        } finally {
            if (statement != null) {
                statement.close();
            }
            if (rs != null) {
                rs.close();
            }
        }

        if ("-1".equals(tmpRespCode)) {
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("当前时间范围内无退票数据");
        }

        retAts.writeEnd();
        return retAts;
    }
}